﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using Resources;
using mojoPortal.Web.Framework;
using System.Collections;
using mojoPortal.Business;
using Telerik.Web.UI;
using ud_gSDMS.Code_Service;

namespace ud_gSDMS.contract
{
    public partial class Contract_Report : mojoBasePage
    {
        private int pageId = -1;
        private int moduleId = -1;

        void Page_Load(object sender, EventArgs e)
        {
            if (SiteUtils.GetCurrentSiteUser()==null)
            {
                WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
            }
            else
            {
                if (!Page.IsPostBack)
                {
                    PopulateLabels();
                    PopulateControls();
                }
            }
        }
        private void PopulateLabels()
        {

        }
        private void PopulateControls()
        {
            datContract_Date_from.SelectedDate = DateTime.Now.AddMonths(-3);
            datContract_Date_to.SelectedDate = DateTime.Now;
            datContract_Date_from.MaxDate = DateTime.Now;
            datContract_Date_to.MaxDate = DateTime.Now;
            LoadControl();
            LoadGrid();
        }
        private void LoadGrid()
        {
            var data = gSDMS_BL.ud_gSDMS_CONTRACT_Search(datContract_Date_from.SelectedDate == null ? null : datContract_Date_from.SelectedDate.Value.ToShortDateString(),
                                                         datContract_Date_to.SelectedDate == null ? null : datContract_Date_to.SelectedDate.Value.ToShortDateString(),
                                                         cboContract_Code.Text.Trim() == "" ? null : cboContract_Code.Text,
                                                         cboContract_Name.Text.Trim() == "" ? null : cboContract_Name.Text,
                                                         cboStatus.Text.Trim() == "" ? null : cboStatus.SelectedValue,
                                                         cboAccount_Manager.Text.Trim() == "" ? null : cboAccount_Manager.SelectedValue,
                                                         chkActive.Checked
                );

            if (ViewState["SortExpression"] != null)
            {
                var dv = data.DefaultView;
                dv.Sort = ViewState["SortExpression"].ToString();
                data = dv.Table;
            }

            grdContract.DataSource = data;
            grdContract.DataBind();

            ViewState["ContractList"] = data;

        }
        private void LoadControl()
        {
            cboContract_Code.DataValueField = "Contract_ID";
            cboContract_Code.DataTextField = "Contract_Code";
            cboContract_Code.DataSource = gSDMS_BL.ud_gSDMS_CONTRACT_Search(null, null, null, null, null, null, true);
            cboContract_Code.DataBind();

            cboContract_Name.DataValueField = "Contract_ID";
            cboContract_Name.DataTextField = "Contract_Name";
            cboContract_Name.DataSource = gSDMS_BL.ud_gSDMS_CONTRACT_Search(null, null, null, null, null, null, true);
            cboContract_Name.DataBind();

            cboStatus.Items.Add(new RadComboBoxItem("", ""));
            cboStatus.Items.Add(new RadComboBoxItem("Not to accept","not-accept"));
            cboStatus.Items.Add(new RadComboBoxItem("Accepted", "accepted"));
            cboStatus.Items.Add(new RadComboBoxItem("Close", "close"));

            cboAccount_Manager.DataValueField = "UserID";
            cboAccount_Manager.DataTextField = "LoginName";
            cboAccount_Manager.DataSource = gSDMS_BL.mp_User_Search(null, null, null, null, null, null, "6");
            cboAccount_Manager.DataBind();

        }

        private void grdContract_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdContract.PageIndex = e.NewPageIndex;
            LoadGrid();
        }
        private void grdContract_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            
            //var row = table.Rows[index];

            if (e.CommandName == "detail")
            {
                int Contract_ID = int.Parse(e.CommandArgument.ToString());
                WebUtils.SetupRedirect(this, SiteRoot + "/gsdmsmodule/contract/Contract_Report_Detail.aspx?pageid=16&id=" + Contract_ID);
            }
        }
        void grdContract_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            LoadGrid();
        }
        void grdContract_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (ViewState["Direction"] == null) ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " ASC") ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " DESC") ViewState["Direction"] = " ASC";
            //grdContract.EditIndex = -1;
            string strSortExp = e.SortExpression + ViewState["Direction"].ToString();
            ViewState["SortExpression"] = strSortExp;
            
            LoadGrid();
        }

        void btnSearch_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            LoadGrid();
        }

        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
            Load += Page_Load;
        }

        protected override void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            base.OnInit(e);
            grdContract.PageIndexChanging += grdContract_PageIndexChanging;
            grdContract.RowCommand += grdContract_RowCommand;
            grdContract.RowDeleting += grdContract_RowDeleting;
            grdContract.Sorting += grdContract_Sorting;

            btnSearch.Click += btnSearch_Click;
            btnExport_Excel.Click +=  btnExport_Excel_Click;
            //btnFinish.Click += btnFinish_Click;
            SuppressPageMenu();
        }

        void btnExport_Excel_Click(object sender, EventArgs e)
        {
            var s = ViewState["ContractList"];
            if (s != null)
            {
                hiddenGrid.DataSource = s;
                hiddenGrid.DataBind();

                var name = ("Contract-List" + datContract_Date_from.SelectedDate.Value.ToShortDateString() + "-" + datContract_Date_to.SelectedDate.Value.ToShortDateString()).Replace("/", "").Replace(":", "").Replace(" ", "_");
                hiddenGrid.ExportSettings.FileName = name;
                hiddenGrid.ExportSettings.IgnorePaging = true;
                hiddenGrid.ExportSettings.OpenInNewWindow = true;
                hiddenGrid.MasterTableView.ExportToExcel();

                hiddenGrid.DataSource = null;
                //    ASPxGridViewExporter1.WriteXlsToResponse(name);

            }
        }

        void btnFinish_Click(object sender, EventArgs e)
        {
            WebUtils.SetupRedirect(this, SiteUtils.GetCurrentPageUrl());
        }

        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", true, moduleId);
        }

        private void LoadPanels()
        {
            Hashtable settings = ModuleSettings.GetModuleSettings(moduleId);
            //config = new ContactFormConfiguration(settings);
            bool showLeftColumnAdmin = ConfigHelper.GetBoolProperty("ShowLeftColumnAdmin", false);
            bool showRightColumnAdmin = ConfigHelper.GetBoolProperty("ShowRightColumnAdmin", false);
            bool showTopColumnAdmin = ConfigHelper.GetBoolProperty("ShowTopColumnAdmin", false);
            bool showBottomColumnAdmin = ConfigHelper.GetBoolProperty("ShowBottomColumnAdmin", false);
            int showCenterColumnAdmin = ConfigHelper.GetIntProperty("ShowCenterColumnAdmin", 0);
            LoadSideContent(showLeftColumnAdmin, showRightColumnAdmin, showTopColumnAdmin, showBottomColumnAdmin, showCenterColumnAdmin);
        }
        #endregion
    }
}